/*
 * @ : Copyright (c) 2021 Phytium Information Technology, Inc. 
 *  
 * SPDX-License-Identifier: Apache-2.0.
 * 
 * @Date: 2021-08-30 08:25:47
 * @LastEditTime: 2021-09-06 14:55:11
 * @Description:  This files is for 
 * 
 * @Modify History: 
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 */
#include <stdio.h>
#include "parameters.h"
#include "pl011_uart.h"
// #include "gicv3.h"
#include "interrupt.h"

Pl011 pl011_obj;
volatile u32 get_uart_rx_flg = 0;
volatile u8 set_irq_priority = 50;

static void UartIrqCallback(void *args, u32 event, u32 event_data)
{
    get_uart_rx_flg = 1;
    Pl011IrqClearReciveTimeOut(&pl011_obj);
}

static void UartIrqHandler(s32 vector, void *param)
{
    Pl011InterruptHandler(param);
}

void UartInit(void)
{
    Pl011Config config = *Pl011LookupConfig(FT_UART1_ID);
    Pl011CfgInitialize(&pl011_obj, &config);
    Pl011SetHandler(&pl011_obj, UartIrqCallback, NULL);
    InterruptSetPriority(pl011_obj.config.irq_num, set_irq_priority);
    InterruptInstall(pl011_obj.config.irq_num, UartIrqHandler, &pl011_obj, "uart1");
    InterruptUmask(pl011_obj.config.irq_num);
    Pl011SetOptions(&pl011_obj, FUART_OPTION_UARTEN | FUART_OPTION_RXEN | FUART_OPTION_TXEN | FUART_OPTION_FIFOEN);
    Pl011IrqEnableReciveTimeOut(&pl011_obj);
}

void TestUart()
{
    char  Byte;

    UartInit();
    while (1)
    {
        while (get_uart_rx_flg == 0)
        {

        }

        Byte = Pl011BlockReceive(&pl011_obj);
        printf("-->recv %c\r\n", Byte);
        Pl011BlockSend(&pl011_obj, &Byte, 1);
        InterruptMask(pl011_obj.config.irq_num);
        set_irq_priority++;
        InterruptSetPriority(pl011_obj.config.irq_num, set_irq_priority);
        InterruptUmask(pl011_obj.config.irq_num);
        get_uart_rx_flg = 0;
        Pl011IrqEnableReciveTimeOut(&pl011_obj);
    }   
}